<template>
	<view>
		<view class="title" style="display: flex;justify-content: space-between;">
			<view class="">
				董事信息（请列出所有董事）
			</view>
			<view class="">
				<u-button size="mini" @click="handAddShare">添加董事</u-button>
			</view>
		</view>
		<view class="form_box">
			<u-form-item>
				<view class="info_list">
					<view class="info_title">
						<view class="info_name title">
							姓名
						</view>
						<view class="info_proportion title">
							类型
						</view>
						<view class="title btn">

						</view>
					</view>
					<view class="info_title" v-for="(item,index) in CompanyDirector" :key='item.guid'>
						<view class="info_name title">
							<u-input :disabled="item.id!=''" v-model="item.userName" placeholder='请输入' />
						</view>
						<view class="info_proportion title">
							<u-input placeholder='选择' v-model="item.identity" type="select" @click="handInfoType(index,item)" />
						</view>
						<view class="title btn">
							<u-button style="margin-right: 20rpx;" size="mini" @click="handFunAdd(item)">确定</u-button>
							<u-button size="mini" @click="handFun(index)">操作</u-button>
						</view>
					</view>
				</view>
			</u-form-item>
		</view>
		<u-action-sheet :list="actionSheetList" v-model="isMainland" @click="actionCallback"></u-action-sheet>
		<u-action-sheet :list="btnsList" v-model="isBtns" @click="btnsClick"></u-action-sheet>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				CompanyDirector: [{
					companyId: '', //公司资料单ID ,
					guid: this.$u.guid(),
					id: '',
					identity: '', //身份 1大陆 2非大陆 ,
					userName: '', //股东姓名
				}],
				share: {},
				isMainland: false,
				isBtns: false,
				btnsList: [
					{
						text: '完善信息',
						id: 2
					},
					{
						text: '删除',
						id: 3,
						color: 'red',
					},
				],
				shareIndex: '',
				actionSheetList: [{
						text: '大陆',
						id: 1
					},
					{
						text: '非大陆',
						id: 2
					}
				],

			}
		},

		methods: {

			//添加股东
			handAddShare() {
				let row = {
					companyId: '', //公司资料单ID ,
					guid: this.$u.guid(),
					id: '',
					identity: '', //身份 1大陆 2非大陆 ,
					shareholding: '', //持股比例 ,
					userName: '', //股东姓名
				}
				this.CompanyDirector.push(row)
			},
			//操作 删除 生成股东 完善信息
			async btnsClick(index) {
				switch (index) {
					case 0: //完善信息
						if (this.share.id) {
							if (this.share.identity == '大陆') {
								uni.navigateTo({
									url: './personal/yesForm/yesForm?directorId=' + this.share.id +
										'&name=addCompanyDirectorInfo&detailsName=findCompanyDirectorById'
								})
							} else if (this.share.identity == '非大陆') {
								uni.navigateTo({
									url: './personal/noForm/noForm?directorId=' + this.share.id +
										'&name=addCompanyDirectorInfo&detailsName=findCompanyDirectorById'
								})
							}
						} else {
							uni.showToast({
								title: '请先生成股东',
								icon: "none"
							})
						}
						break

					case 1:
						if (this.share.id) {
							console.log(this.share.id)
							await this.$u.api.delCompanyDirector({
								id: this.share.id
							})
							this.CompanyDirector.forEach((item, index) => {
								if (item.guid == this.share.guid) {
									this.CompanyDirector.splice(index, 1)
								}
							})
						} else {
							this.CompanyDirector.forEach((item, index) => {
								if (item.guid == this.share.guid) {
									this.CompanyDirector.splice(index, 1)
								}
							})
						}
						break
					default:
						break;
				}
			},
			async handFunAdd(row) {
				this.share = this.$u.deepClone(row);
				if (this.share.id) {
					uni.showToast({
						title: '该董事已成生，请勿重复生成！！'
					})
					return
				}
				if (!this.share.userName) {
					this.isBtns = false
					uni.showToast({
						title: '请输入董事姓名',
						icon: 'none'
					})
					return
				}
				if (!this.share.identity) {
					this.isBtns = false
					uni.showToast({
						title: '请选择董事类型',
						icon: 'none'
					})
					return
				}
				this.share.identity = this.share.identity == '大陆' ? 1 : 2
				const res = await this.$u.api.addCompanyDirector(this.share)
				this.CompanyDirector.forEach(item => {
					if (item.guid == this.share.guid) {
						item.id = res
					}
				})
				uni.showToast({
					title: '生成成功'
				})

			},
			//唤起操作栏
			handFun(index) {
				this.share = this.$u.deepClone(this.CompanyDirector[index]);
				this.isBtns = true
			},
			//获取股东类别（大陆非大陆）
			handInfoType(shareIndex, row) {
				if (row.id) {
					return
				}
				this.shareIndex = shareIndex
				this.isMainland = true
			},
			actionCallback(index) {
				this.CompanyDirector[this.shareIndex].identity = this.actionSheetList[index].text
			},

		}
	}
</script>

<style lang="scss" scoped>
	.info_list {
		display: flex;
		flex-direction: column;
		width: 100%;
		.info_title {
			display: flex;
			justify-content: space-between;

			.btn {
				display: flex;
				justify-content: center;
				padding: 0 20rpx;
				box-sizing: border-box;
			}
		}
	}

	.form_box {
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.title {
		background-color: #f7f7f7;
		width: 100%;
		height: 60rpx;
		line-height: 60rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
	}
</style>
